package autocode

import (
	"time"

	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	autoCodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
)

type MarksBillService struct {
}

// CreateMarksBill 创建MarksBill记录
// Author [piexlmax](https://github.com/piexlmax)
func (marksBillService *MarksBillService) CreateMarksBill(marksBill autocode.MarksBill) (err error) {
	err = global.GVA_DB.Create(&marksBill).Error
	return err
}

// DeleteMarksBill 删除MarksBill记录
// Author [piexlmax](https://github.com/piexlmax)
func (marksBillService *MarksBillService) DeleteMarksBill(marksBill autocode.MarksBill) (err error) {
	err = global.GVA_DB.Delete(&marksBill).Error
	return err
}

// DeleteMarksBillByIds 批量删除MarksBill记录
// Author [piexlmax](https://github.com/piexlmax)
func (marksBillService *MarksBillService) DeleteMarksBillByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]autocode.MarksBill{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateMarksBill 更新MarksBill记录
// Author [piexlmax](https://github.com/piexlmax)
func (marksBillService *MarksBillService) UpdateMarksBill(marksBill autocode.MarksBill) (err error) {
	err = global.GVA_DB.Save(&marksBill).Error
	return err
}

// GetMarksBill 根据id获取MarksBill记录
// Author [piexlmax](https://github.com/piexlmax)
func (marksBillService *MarksBillService) GetMarksBill(id uint) (err error, marksBill autocode.MarksBill) {
	err = global.GVA_DB.Where("id = ?", id).First(&marksBill).Error
	return
}

// GetMarksBillInfoList 分页获取MarksBill记录
// Author [piexlmax](https://github.com/piexlmax)
func (marksBillService *MarksBillService) GetMarksBillInfoList(info autoCodeReq.MarksBillSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&autocode.MarksBill{}).Order("create_time desc")
	var marksBills []autocode.MarksBill
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.EndData != nil && info.StartData != nil {
		db.Where("marks_bill.create_time >= ? AND marks_bill.create_time <= ?", time.Unix(info.StartData.Unix(), 0), time.Unix(info.EndData.Unix()+86400-1, 0))
	}
	if info.UserId != nil && *info.UserId != 0 {
		db = db.Where("marks_bill.user_id = ?", info.UserId)
	}
	//if info.InviteId != nil && *info.UserId != 0 {
	//	db = db.Where("invite_id = ?", info.UserId)
	//}
	if info.Type != nil && *info.Type != 0 {
		db = db.Where("marks_bill.type = ?", info.Type)
	}
	if info.Channel != nil && *info.Channel != 0 {
		db = db.Where("marks_bill.channel = ?", info.Channel)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	selects := "user_info.user_name, marks_bill.user_id, marks_bill.created_at, marks_bill.create_time, marks_bill.num, marks_bill.channel, marks_bill.source, marks_bill.type, user_info.open_id, marks_bill.invite_id"
	err = db.Limit(limit).Offset(offset).Joins("left join user_info on user_info.id = marks_bill.invite_id").Select(selects).Find(&marksBills).Error

	return err, marksBills, total
}
